package com.example.opengldemo._11_;

import android.app.Activity;
import android.opengl.GLES30;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.opengldemo.R;
import com.example.opengldemo._11_.test.TextureImageView;

import java.util.ArrayList;
import java.util.List;
// 1 创建管线并获取索引
// 2 创建数据并格式话为buffer
// 3 进行绘制       1 激活管线
//                 2 索引链接数据
//                 3 纹理激活绑定(多个纹理需链接索引)
//                 4 设置顶点着色器索引可用
//                 5 绘制
//                 6 设置顶点着色器索引不可用
public class MixingActivity extends Activity {
    Button button1;
    Button button2;
    Button button3;

    ListView listView11;
    ListView listView12;
    ListView listView21;
    ListView listView22;
    List<String> list = new ArrayList<>();
    List<String> list2 = new ArrayList<>();
    ArrayAdapter<String> adapter11;
    ArrayAdapter<String> adapter12;
    ArrayAdapter<String> adapter21;
    ArrayAdapter<String> adapter22;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mixing);
        initData();
        initView();

    }

    private void initView() {
        button1 = findViewById(R.id.model);
        button2 = findViewById(R.id.model1);
        button3 = findViewById(R.id.model2);

        listView11 = findViewById(R.id.list_11);
        listView12 = findViewById(R.id.list_12);
        listView21 = findViewById(R.id.list_21);
        listView22 = findViewById(R.id.list_22);
        listView11.setAdapter(adapter11);
        listView12.setAdapter(adapter12);
        listView21.setAdapter(adapter21);
        listView22.setAdapter(adapter22);

        listView11.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String s = ((TextView) view).getText().toString();
                TextView viewById = findViewById(R.id.tv_11);
                if(s == null ) return ;
                viewById.setText(s);
                TextureView.srcTextureState = getState(s);
            }
        });
        listView12.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String s = ((TextView) view).getText().toString();
                TextView viewById = findViewById(R.id.tv_12);
                if(s == null ) return ;
                viewById.setText(s);
            }
        });
        listView21.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String s = ((TextView) view).getText().toString();
                TextView viewById = findViewById(R.id.tv_21);
                if(s == null ) return ;
                viewById.setText(s);
                TextureView.postTextuteState = getState(s);
            }
        });
        listView22.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String s = ((TextView) view).getText().toString();
                TextView viewById = findViewById(R.id.tv_22);
                if(s == null ) return ;
                viewById.setText(s);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = list2.get(a % 5);
                ((Button)view).setText(s);
                a++ ;
                TextureView.equation = getEquation(s);

            }
        });
        LinearLayout viewById = findViewById(R.id.texture_mixing);
        viewById.addView(initTextureView());
    }


    private int getState(String s) {
        int state = GLES30.GL_ZERO ;
        switch (s){
            case "ZERO":
                state = GLES30.GL_ZERO ;
                break ;
            case "ONE":
                state = GLES30.GL_ONE ;
                break ;
            case "SRC_COLOR":
                state = GLES30.GL_SRC_COLOR ;
                break ;
            case "ONE_MINUS_SRC_COLOR":
                state = GLES30.GL_ONE_MINUS_SRC_COLOR ;
                break ;
            case "DST_COLOR":
                state = GLES30.GL_DST_COLOR ;
                break ;
            case "ONE_MINUS_DST_COLOR":
                state = GLES30.GL_ONE_MINUS_DST_COLOR ;
                break ;
            case "SRC_ALPHA":
                state = GLES30.GL_SRC_ALPHA ;
                break ;
            case "ONE_MINUS_SRC_ALPHA":
                state = GLES30.GL_ONE_MINUS_SRC_ALPHA ;
                break ;
            case "DST_ALPHA":
                state = GLES30.GL_DST_ALPHA ;
                break ;
            case "ONE_MINUS_DST_ALPHA":
                state = GLES30.GL_ONE_MINUS_DST_ALPHA ;
                break ;
            case "SRC_ALPHA_SATURATE":
                state = GLES30.GL_SRC_ALPHA_SATURATE ;
                break ;
            case "CONSTANT_COLOR":
                state = GLES30.GL_CONSTANT_COLOR ;
                break ;
            case "ONE_MINUS_CONSTANT_COLOR":
                state = GLES30.GL_ONE_MINUS_CONSTANT_COLOR ;
                break ;
            case "CONSTANT_ALPHA":
                state = GLES30.GL_CONSTANT_ALPHA ;
                break ;
            case "ONE_MINUS_CONSTANT_ALPHA":
                state = GLES30.GL_ONE_MINUS_CONSTANT_ALPHA ;
                break ;
        }
        return state ;
    }

    private int getEquation(String s) {
        int state = GLES30.GL_FUNC_ADD ;
        switch (s){
            case "FUNC_ADD":
                state = GLES30.GL_FUNC_ADD ;
                break ;
            case "FUNC_SUBTRACT":
                state = GLES30.GL_FUNC_SUBTRACT ;
                break ;
            case "FUNC_REVERSE_SUBTRACT":
                state = GLES30.GL_FUNC_REVERSE_SUBTRACT ;
                break ;
            case "MIN":
                state = GLES30.GL_MIN ;
                break ;
            case "MAX":
                state = GLES30.GL_MAX ;
                break ;
        }
        return state ;
    }

    TextureView textureView ;
    TextureImageView textureImageView ;
    private View initTextureView() {
        textureView = new TextureView(this) ;
        textureView.requestFocus(); //获取焦点
        textureView.setFocusableInTouchMode(true); //设置为可触控
        return textureView;
    }

    private View initTextureImageView() {
        textureImageView = new TextureImageView(this) ;
        textureImageView.requestFocus(); //获取焦点
        textureImageView.setFocusableInTouchMode(true); //设置为可触控
        return textureImageView;
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(textureView != null) {textureView.onPause();}
        if(textureImageView != null) {textureImageView.onPause();}

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(textureView != null) {textureView.onResume();}
        if(textureImageView != null) {textureImageView.onResume();}

    }

    int a = 0 ;
    private void initData() {
        list.add("ZERO");
        list.add("ONE");
        list.add("SRC_COLOR");
        list.add("ONE_MINUS_SRC_COLOR");
        list.add("DST_COLOR");
        list.add("ONE_MINUS_DST_COLOR");
        list.add("SRC_ALPHA");
        list.add("ONE_MINUS_SRC_ALPHA");
        list.add("DST_ALPHA");
        list.add("ONE_MINUS_DST_ ALPHA");
        list.add("SRC_ALPHA_SATURATE");
        list.add("CONSTANT_COLOR");
        list.add("ONE_MINUS_CONSTANT_COLOR");
        list.add("CONSTANT_ALPHA");
        list.add("ONE_MINUS_CONSTANT_ALPHA");
        String[] array = new String[list.size()];
        list.toArray(array);
        list2.add("FUNC_ADD");
        list2.add("FUNC_SUBTRACT");
        list2.add("FUNC_REVERSE_SUBTRACT");
        list2.add("MIN");
        list2.add("MAX");

        adapter11 = new ArrayAdapter<String>(this, R.layout.item_mixing, array);
        adapter12 = new ArrayAdapter<String>(this, R.layout.item_mixing, array);
        adapter21 = new ArrayAdapter<String>(this, R.layout.item_mixing, array);
        adapter22 = new ArrayAdapter<String>(this, R.layout.item_mixing, array);
    }

}
